#include<bits/stdc++.h>
#define N 1010
using namespace std;
struct qq{
	int num[7],cnt;
}a[N];
string s1,s2,s[7];
int n,flag,now,tot,f[7];
void fu(int &x,string y,int z)
{
	if(y.length()>5)x=-1;
	else
	{
		for(int i=0;i<y.length();i++)
			x=x*10+y[i]-48;
		if(z==0&&x>255||x>65535)x=-1;
	}
}
int main()
{
	freopen("network.in","r",stdin);
	freopen("network.out","w",stdout);
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<6;j++)f[j]=0,s[j]="";
		flag=0;now=1;
		cin>>s1>>s2;
		for(int j=0;j<s2.length();j++)
		{
			if(s2[j]<'0'||s2[j]>'9')
			{
				if((now<4&&s2[j]=='.'||now==4&&s2[j]==':')&&s2[j-1]>='0'&&s2[j-1]<='9')now++;
				else{flag=1;break;}
			}
			else s[now]+=s2[j];
		}
		if(now<5||s2[s2.length()-1]==':')flag=1;
		for(int j=1;j<=5;j++)
		{
			fu(f[j],s[j],j==5);
			if(f[j]==-1){flag=1;break;}
		}
		if(flag==1){cout<<"ERR\n";continue;}
		if(s1=="Server")
		{
			for(int j=1;j<=tot;j++)
				if(f[1]==a[j].num[1]&&f[2]==a[j].num[2]&&f[3]==a[j].num[3]&&f[4]==a[j].num[4]&&f[5]==a[j].num[5])
					{flag=1;break;}
			if(flag==1)cout<<"FAIL\n";
			else
			{
				a[++tot].cnt=i;
				for(int j=1;j<=5;j++)a[tot].num[j]=f[j];
				cout<<"OK\n";
			}
		}
		else
		{
			for(int j=1;j<=tot;j++)
				if(f[1]==a[j].num[1]&&f[2]==a[j].num[2]&&f[3]==a[j].num[3]&&f[4]==a[j].num[4]&&f[5]==a[j].num[5])
					{flag=j;break;}
			if(flag!=0)
			{
				cout<<a[flag].cnt<<'\n';
			}
			else cout<<"FAIL\n";
		}
	}
}
